package com.app.ytf.httpdemo.acvitity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.app.ytf.httpdemo.R;
import com.app.ytf.httpdemo.okhttp.MyDataCallBack;
import com.app.ytf.httpdemo.okhttp.OkHTTPManger;
import com.app.ytf.httpdemo.util.Constant;
import com.app.ytf.httpdemo.util.ThreadPoolUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author ytf
 */
public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";
    @BindView(R.id.tv_content)
    TextView tvContent;
    String res;
    @BindView(R.id.pro_bar)
    ProgressBar proBar;
    @BindView(R.id.btn_jump)
    Button btnJump;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
//        OkHttpClient okHttpClient=OkHTTPClientUtil.getInstance(this).getOkHttpClient();
//        OkHTTPClientUtil.getInstance(this).getRequest();


    }

    private void postDouBan() {
        //1,创建OKHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2,创建一个Request
        RequestBody requestBody = new FormBody.Builder()
                .add("username", "ytf")
                .add("password", "123")
                .build();
        Request request = new Request.Builder().url(Constant.URL).post(requestBody).build();
        //4,将请求添加到调度中
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    try {
                        res = response.body().string();
                        ThreadPoolUtil.execute(new Runnable() {
                            @Override
                            public void run() {
//                                Looper.prepare();
//                                Toast.makeText(MainActivity.this, res, Toast.LENGTH_SHORT).show();
                                Log.d(TAG, res);
                                EventBus.getDefault().post(res);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void getDouBan() {
        //1,创建OKHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2,创建一个Request
        Request request = new Request.Builder().url(Constant.URL).build();
        //3,创建一个call对象
        Call call = okHttpClient.newCall(request);
        //4,将请求添加到调度中
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful()) {
                    try {
                        res = response.body().string();
                        ThreadPoolUtil.execute(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, res);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    private void getDouBan1() {
        //1,创建OKHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2,创建一个Request
        Request request = new Request.Builder().url(Constant.URL).build();
        //3,创建一个call对象
        Call call = okHttpClient.newCall(request);
        //4,将请求添加到调度中
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful()) {
                    try {
                        res = response.body().string();
                        ThreadPoolUtil.execute(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, res);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    public void btn_get(View view) {
        proBar.setVisibility(View.VISIBLE);
        Map<String,String> parms=new HashMap<>();
        parms.put("start","0");
        parms.put("count","10");

        OkHTTPManger.getInstance().postAsynRequireJson(Constant.URLPATH, parms, new MyDataCallBack() {
            @Override
            public void onBefore(Request request) {

            }


            @Override
            public void requestSuccess(Object result) {
                EventBus.getDefault().post(result);
            }

            @Override
            public void requestFailure(Request request, IOException e) {

            }

            @Override
            public void onAfter() {

            }
        });
//        OkHTTPManger.getInstance().postAsynBackString(Constant.URLPATH, parms, new MyDataCallBack() {
//            @Override
//            public void onBefore(Request request) {
//
//            }
//
//            @Override
//            public void requestSuccess(String result) {
//                EventBus.getDefault().post(result);
//            }
//
//            @Override
//            public void requestFailure(Request request, IOException e) {
//
//            }
//
//            @Override
//            public void onAfter() {
//
//            }
//        });
//        OkHTTPManger.getInstance().getAsynBackStringWithParms(Constant.URLPATH, parms, new MyDataCallBack() {
//            @Override
//            public void onBefore(Request request) {
//
//            }
//
//            @Override
//            public void requestSuccess(String result) {
//                EventBus.getDefault().post(result);
//            }
//
//            @Override
//            public void requestFailure(Request request, IOException e) {
//
//            }
//
//            @Override
//            public void onAfter() {
//
//            }
//        });
//
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showMsg(String res) {
        if (res != null) {
            tvContent.setText(res);
            proBar.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.btn_jump)
    public void onViewClicked() {
        SecondActivity.actionIntent(this,"ytf","111");
    }
}
